//
//  TSProgressTableView.m
//  chart
//
//  Created by 曾斌 on 16/4/11.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSProgressTableView.h"
#import "TSMsgContentItemCell.h"
#import "TSMsgDatePickerTextFieldCell.h"
#import "TSPickerViewTextFieldCell.h"
#import "FBFilesTableViewController.h"
#import "TaskItemCell.h"
#import "TSTaskProgressModel.h"
#import "TSTaskIngDetailController.h"
#import "TSProgressDetailController.h"
@implementation TSProgressTableView{
    NSArray *rowsArray;
    BOOL isManualScroll;
    CGPoint point;
    TSTaskListModel *progressModel;
}

@synthesize taskId;

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
    }
     progressModel = [[TSTaskListModel alloc] init];
    
    return self;
}

//指定有多少个分区(Section)，默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(progressModel.list ){
        return [progressModel.list count];//返回标题数组中元素的个数来确定分区的个数
    }
    return 0;
    
}


//每个section显示的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [NSString stringWithFormat:@"%@%d%@",@"第",section+1,@"阶段"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if(progressModel.list&& [progressModel.list objectAtIndex:section]){
        return [[progressModel.list objectAtIndex:section] count];
    }else{
        return 0;}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return CELL_HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
        static NSString *cell_identify = @"cellIdentify";
        TaskItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
        if (!cell) {
            cell = [[TaskItemCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
        }
        if(progressModel&&progressModel.list&&[progressModel.list objectAtIndex:indexPath.section]){
        TSTaskModel * model = [[progressModel.list objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        
        if (model) {
            [cell setModel:model];
        }
        }
        return cell;
    
}

#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    progressModel.requestParams = [self createRequestParam];
    [progressModel fetchWithPath:kRequestUrl_Task_outList_detail_splitList completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        strongSelf.footer.hidden = NO;
       [weakSelf reloadData];
        
    } NoProgress:NO];
}

-(NSMutableDictionary *)createRequestParam{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:10];
    [dic setObject:@(self.pageIndex) forKey:TSDictionaryKeyDefine_PageIndex];
    [dic setObject:@(self.pageSize) forKey:TSDictionaryKeyDefine_PageSize];
    [dic setObject:taskId forKey:TSDictionaryKeyDefine_Id];

    return dic;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(progressModel.list&&[progressModel.list objectAtIndex:indexPath.section]){
    
        TSTaskModel *model = [[progressModel.list objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        if(model){
        [self toNormalController:model];
        }
    }
    
    
}

-(void)toNormalController:(TSTaskModel*)model{
    TSProgressDetailController *detail_vc = [[TSProgressDetailController alloc] init];
    detail_vc.taskModel = model;
    detail_vc.taskId = model.tId;
    [[self getViewController].navigationController pushViewController:detail_vc animated:YES];
}
@end

